package com.turkcell.dv.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;

import com.turkcell.dv.model.ProcessPrivilegeDOM;

import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@Transactional(readOnly = true)
public class ProcessPrivilegeDAOImpl implements ProcessPrivilegeDAO{

	private static final Logger logger = LoggerFactory.getLogger(ProcessPrivilegeDAOImpl.class);
	 
	@Autowired private SessionFactory sessionFactory;
    
    public void setSessionFactory(SessionFactory sf){
        this.sessionFactory = sf;
    }


	@Override
	public void addProcessPrivilege(ProcessPrivilegeDOM pp) {
		Session session = this.sessionFactory.getCurrentSession();
        session.persist(pp);
        logger.info("ProcessPrivilege saved successfully, ProcessPrivilege Details= " + pp);
    }

	@Override
	public void updateProcessPrivilege(ProcessPrivilegeDOM pp) {
		Session session = this.sessionFactory.getCurrentSession();
        session.update(pp);
        logger.info("ProcessPrivilege updated successfully, ProcessPrivilege Details=" + pp);
    }

	@SuppressWarnings("unchecked")
	@Override
	public List<ProcessPrivilegeDOM> listProcessPrivileges() {
        Session session = this.sessionFactory.getCurrentSession();
        List<ProcessPrivilegeDOM> ProcessPrivilegesList = session.createQuery("from ProcessPrivilegeDOM").list();
        for(ProcessPrivilegeDOM pp : ProcessPrivilegesList){
            logger.info("ProcessPrivilege List::" + pp);
        }
        return ProcessPrivilegesList;
	}

	@Override
	public ProcessPrivilegeDOM getProcessPrivilegeById(long id) {
		Session session = this.sessionFactory.getCurrentSession();
		ProcessPrivilegeDOM pp = (ProcessPrivilegeDOM) session.load(ProcessPrivilegeDOM.class, new Long(id));
		logger.info("ProcessPrivilege loaded successfully, ProcessPrivilege details=" + pp);
		return pp;
	}

	@Override
	public void removeProcessPrivilege(long id) {
        Session session = this.sessionFactory.getCurrentSession();
        ProcessPrivilegeDOM pp = (ProcessPrivilegeDOM) session.load(ProcessPrivilegeDOM.class, new Long(id));
        if(null != pp){
            session.delete(pp);
        }
        logger.info("ProcessPrivilege deleted successfully, ProcessPrivilege details=" + pp);
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<ProcessPrivilegeDOM> getBusinessProcessIdListByPrivilegeId(long privilegeId) {
        Session session = this.sessionFactory.getCurrentSession();
		Query query = session.createQuery("from ProcessPrivilegeDOM PP WHERE PP.privilegeId = :privilegeId");
		query.setParameter("privilegeId", privilegeId);
		return query.list();
	}
}
